home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Net / Netrc.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  6.4 KB  |  309 lines

  1.  
  2. package Net::Netrc;
  3.  
  4. use Carp;
  5. use strict;
  6. use FileHandle;
  7. use vars qw($VERSION);
  8.  
  9. $VERSION = do { my @r=(q$Revision: 2.6 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
  10.  
  11. my %netrc = ();
  12.  
  13. sub _readrc
  14. {
  15.  my $host = shift;
  16.  
  17.  my $home = ($^O =~ /mswin32/i ? $ENV{HOME} :
  18.                   (eval { (getpwuid($>))[7] } || $ENV{HOME}));
  19.  my $file = $home . "/.netrc";
  20.  
  21.  my($login,$pass,$acct) = (undef,undef,undef);
  22.  my $fh;
  23.  local $_;
  24.  
  25.  $netrc{default} = undef;
  26.  
  27.  unless($^O eq 'os2')
  28.   { 
  29.    my @stat = stat($file);
  30.  
  31.    if(@stat)
  32.     {
  33.      if($stat[2] & 077)
  34.       {
  35.        carp "Bad permissions: $file";
  36.        return;
  37.       }
  38.      if($stat[4] != $<)
  39.       {
  40.        carp "Not owner: $file";
  41.        return;
  42.       }
  43.     }
  44.   }
  45.  
  46.  if($fh = FileHandle->new($file,"r"))
  47.   {
  48.    my($mach,$macdef,$tok,@tok) = (0,0);
  49.  
  50.    while(<$fh>)
  51.     {
  52.      undef $macdef if /\A\n\Z/;
  53.  
  54.      if($macdef)
  55.       {
  56.        push(@$macdef,$_);
  57.        next;
  58.       }
  59.  
  60.      push(@tok, split(/[\s\n]+/, $_));
  61.  
  62. TOKEN:
  63.      while(@tok)
  64.       {
  65.        if($tok[0] eq "default")
  66.         {
  67.          shift(@tok);
  68.          $mach = bless {};
  69.         $netrc{default} = [$mach];
  70.  
  71.          next TOKEN;
  72.         }
  73.  
  74.        last TOKEN
  75.             unless @tok > 1;
  76.  
  77.        $tok = shift(@tok);
  78.  
  79.        if($tok eq "machine")
  80.         {
  81.          my $host = shift @tok;
  82.          $mach = bless {machine => $mach};
  83.  
  84.          $netrc{$host} = []
  85.             unless exists($netrc{$host});
  86.          push(@{$netrc{$host}}, $mach);
  87.         }
  88.        elsif($tok =~ /^(login|password|account)$/)
  89.         {
  90.          next TOKEN unless $mach;
  91.          my $value = shift @tok;
  92.          $mach->{$1} = $value;
  93.         }
  94.        elsif($tok eq "macdef")
  95.         {
  96.          next TOKEN unless $mach;
  97.          my $value = shift @tok;
  98.          $mach->{macdef} = {}
  99.             unless exists $mach->{macdef};
  100.          $macdef = $mach->{machdef}{$value} = [];
  101.         }
  102.       }
  103.     }
  104.    $fh->close();
  105.   }
  106. }
  107.  
  108. sub lookup
  109. {
  110.  my($pkg,$mach,$login) = @_;
  111.  
  112.  _readrc()
  113.     unless exists $netrc{default};
  114.  
  115.  $mach ||= 'default';
  116.  undef $login
  117.     if $mach eq 'default';
  118.  
  119.  if(exists $netrc{$mach})
  120.   {
  121.    if(defined $login)
  122.     {
  123.      my $m;
  124.      foreach $m (@{$netrc{$mach}})
  125.       {
  126.        return $m
  127.             if(exists $m->{login} && $m->{login} eq $login);
  128.       }
  129.      return undef;
  130.     }
  131.    return $netrc{$mach}->[0]
  132.   }
  133.  
  134.  return $netrc{default}->[0]
  135.     if defined $netrc{default};
  136.  
  137.  return undef;
  138. }
  139.  
  140. sub login
  141. {
  142.  my $me = shift;
  143.  
  144.  exists $me->{login}
  145.     ? $me->{login}
  146.     : undef;
  147. }
  148.  
  149. sub account
  150. {
  151.  my $me = shift;
  152.  
  153.  exists $me->{account}
  154.     ? $me->{account}
  155.     : undef;
  156. }
  157.  
  158. sub password
  159. {
  160.  my $me = shift;
  161.  
  162.  exists $me->{password}
  163.     ? $me->{password}
  164.     : undef;
  165. }
  166.  
  167. sub lpa
  168. {
  169.  my $me = shift;
  170.  ($me->login, $me->password, $me->account);
  171. }
  172.  
  173. 1;
  174.  
  175. __END__
  176.  
  177. =head1 NAME
  178.  
  179. Net::Netrc - OO interface to users netrc file
  180.  
  181. =head1 SYNOPSIS
  182.  
  183.     use Net::Netrc;
  184.     
  185.     $mach = Net::Netrc->lookup('some.machine');
  186.     $login = $mach->login;
  187.     ($login, $password, $account) = $mach->lpa;
  188.  
  189. =head1 DESCRIPTION
  190.  
  191. C<Net::Netrc> is a class implementing a simple interface to the .netrc file
  192. used as by the ftp program.
  193.  
  194. C<Net::Netrc> also implements security checks just like the ftp program,
  195. these checks are, first that the .netrc file must be owned by the user and 
  196. second the ownership permissions should be such that only the owner has
  197. read and write access. If these conditions are not met then a warning is
  198. output and the .netrc file is not read.
  199.  
  200. =head1 THE .netrc FILE
  201.  
  202. The .netrc file contains login and initialization information used by the
  203. auto-login process.  It resides in the user's home directory.  The following
  204. tokens are recognized; they may be separated by spaces, tabs, or new-lines:
  205.  
  206. =over 4
  207.  
  208. =item machine name
  209.  
  210. Identify a remote machine name. The auto-login process searches
  211. the .netrc file for a machine token that matches the remote machine
  212. specified.  Once a match is made, the subsequent .netrc tokens
  213. are processed, stopping when the end of file is reached or an-
  214. other machine or a default token is encountered.
  215.  
  216. =item default
  217.  
  218. This is the same as machine name except that default matches
  219. any name.  There can be only one default token, and it must be
  220. after all machine tokens.  This is normally used as:
  221.  
  222.     default login anonymous password user@site
  223.  
  224. thereby giving the user automatic anonymous login to machines
  225. not specified in .netrc.
  226.  
  227. =item login name
  228.  
  229. Identify a user on the remote machine.  If this token is present,
  230. the auto-login process will initiate a login using the
  231. specified name.
  232.  
  233. =item password string
  234.  
  235. Supply a password.  If this token is present, the auto-login
  236. process will supply the specified string if the remote server
  237. requires a password as part of the login process.
  238.  
  239. =item account string
  240.  
  241. Supply an additional account password.  If this token is present,
  242. the auto-login process will supply the specified string
  243. if the remote server requires an additional account password.
  244.  
  245. =item macdef name
  246.  
  247. Define a macro. C<Net::Netrc> only parses this field to be compatible
  248. with I<ftp>.
  249.  
  250. =back
  251.  
  252. =head1 CONSTRUCTOR
  253.  
  254. The constructor for a C<Net::Netrc> object is not called new as it does not
  255. really create a new object. But instead is called C<lookup> as this is
  256. essentially what it does.
  257.  
  258. =over 4
  259.  
  260. =item lookup ( MACHINE [, LOGIN ])
  261.  
  262. Lookup and return a reference to the entry for C<MACHINE>. If C<LOGIN> is given
  263. then the entry returned will have the given login. If C<LOGIN> is not given then
  264. the first entry in the .netrc file for C<MACHINE> will be returned.
  265.  
  266. If a matching entry cannot be found, and a default entry exists, then a
  267. reference to the default entry is returned.
  268.  
  269. =back
  270.  
  271. =head1 METHODS
  272.  
  273. =over 4
  274.  
  275. =item login ()
  276.  
  277. Return the login id for the netrc entry
  278.  
  279. =item password ()
  280.  
  281. Return the password for the netrc entry
  282.  
  283. =item account ()
  284.  
  285. Return the account information for the netrc entry
  286.  
  287. =item lpa ()
  288.  
  289. Return a list of login, password and account information fir the netrc entry
  290.  
  291. =back
  292.  
  293. =head1 AUTHOR
  294.  
  295. Graham Barr <gbarr@ti.com>
  296.  
  297. =head1 SEE ALSO
  298.  
  299. L<Net::Netrc>
  300. L<Net::Cmd>
  301.  
  302. =head1 COPYRIGHT
  303.  
  304. Copyright (c) 1995-1997 Graham Barr. All rights reserved.
  305. This program is free software; you can redistribute it and/or modify
  306. it under the same terms as Perl itself.
  307.  
  308. =cut
  309.